﻿public class Solution {
    public int MaxProfit(int[] prices)
    {
        // 持有、不持有
        int[,] dp = new int[prices.Length, 2];
        dp[0, 0] = -prices[0];
        dp[0, 1] = 0;

        for (int i = 1; i < prices.Length; i++)
        {
            dp[i, 0] = Math.Max(dp[i - 1, 0], dp[i-1, 1] - prices[i]);
            dp[i, 1] = Math.Max(dp[i - 1, 1], dp[i-1, 0] + prices[i]);
        }
        return dp[prices.Length-1, 1];
    }

    public static void Main(string[] args)
    {
        int[] prices = [7, 1, 5, 3, 6, 4];
        Solution solution = new Solution();
        int res = solution.MaxProfit(prices);
        Console.WriteLine(res);
    }
}